#!/bin/env ruby
# encoding: utf-8
require_relative '../spec/spec_helper'
require_relative '../spec/commons/user_auth'
require_relative '../spec/commons/check_pages'

describe 'Sign Up page' do
	before {

	}
 
  context 'when open sign_up_page' do
  	it 'then it shoul be correct' do
  		goto 'http://pulse.7bits.it/en/us/sign-up'
  		check_signup_page()      
    end
  end
 
  ##   - first name: 
  #  -- submit empty input:  “First Name can't be blank”
  context 'when submit empty first name' do
    it 'then First Name cannot be blank' do
      text_field(name: "firstName", class: "form-control").set ""
      button(type: "submit",  class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "First Name can't be blank").wait_until_present(5)
    end
  end

  #  -- submit string with length more than 255:  “First name should contain no more than 255 symbols”
  context 'when submit first name' do
    it 'then it should contain no more than 255 symbols' do
      text_field(class: "form-control", name: "firstName").set str(256)
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "First name should contain no more than 255 symbols").wait_until_present(5)     
    end
  end

  ##  - last name: 
  #  -- submit empty input:  “Last Name can't be blank”
  context 'when submit empty last name' do
    it 'then Last Name cannot be blank' do
      text_field(name: "lastName",  class: "form-control").set ""
      button(type: "submit",  class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Last Name can't be blank").wait_until_present(5)
    end
  end
  
  #  -- submit string with length more than 255:  “Last name should contain no more than 255 symbols”
  context 'when submit last name' do
    it 'then it should contain no more than 255 symbols' do
      text_field(class: "form-control", name: "lastName").set str(256)
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Last name should contain no more than 255 symbols").wait_until_present(5)     
    end
  end

  ##  - Username (email): 
  # -- submit empty input:  “Email can't be blank”
  context 'when submit empty email' do
    it 'then email cannot be blank' do
      text_field(class: "form-control", name: "username").clear
      button(type: "submit",  class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Email can't be blank").should be_present
    end
  end

  #  -- submit string with length more than 255:  “Email  should contain no more than 255 symbols”
  context 'when submit email' do
    it 'then it should contain no more than 255 symbols' do      
      ustr=str(255) + '@list.ru'
      text_field(class: "form-control", name: "username").set ustr
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Email should contain no more than 255 symbols").wait_until_present(10)
    end
  end

  #  -- submit invalid email: “Invalid email format”
  #TODO уточноить критерии проверки на валидность
  context 'when submit username' do
    it 'then it should has valid email format' do
      #примеры невалидных адресов
       text_field(class: "form-control", name: "username").set "12345"
       button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
       span(class: "help-block", text: "Invalid email format").wait_until_present(10)      

       text_field(class: "form-control", name: "username").set "_)(@++sd)"
       button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
       span(class: "help-block", text: "Invalid email format").wait_until_present(10) 
       
       text_field(class: "form-control", name: "username").set "12345@"
       button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
       span(class: "help-block", text: "Invalid email format").wait_until_present(5)      
       
       text_field(class: "form-control", name: "username").set "wrtyu@wrtyu"
       button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
       span(class: "help-block", text: "Invalid email format").wait_until_present(5)      

       #text_field(class: "form-control", name: "username").set "rtyui@reyui.ryuio.rrrrrr"
       #button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
       #span(class: "help-block", text: "Invalid email format").wait_until_present(5)      
       
       text_field(class: "form-control", name: "username").set "@dyu"
       button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
       span(class: "help-block", text: "Invalid email format").wait_until_present(5)   
                    
       text_field(class: "form-control", name: "username").set "@@sjkl.cot"
       button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
       span(class: "help-block", text: "Invalid email format").wait_until_present(5)   

       text_field(class: "form-control", name: "username").set "rre@sjkl.cot.rr."
       button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
       span(class: "help-block", text: "Invalid email format").wait_until_present(5)   
     end
   end
  
  #  -- submit personal email (gmail, yahoo, hotmail etc): “Please use company email. Personal emails are not allowed as admin!”
  context 'when submit username' do
    it 'then it should be company email' do
      text_field(class: "form-control", name: "username").set "julia.ashaeva@gmail.com"
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Please use company email. Personal emails are not allowed as admin!").wait_until_present      

      text_field(class: "form-control", name: "username").set "ashaeva@yahoo.com"
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Please use company email. Personal emails are not allowed as admin!").wait_until_present     

      text_field(class: "form-control", name: "username").set "ashaeva@hotmail.com"
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click    
      span(class: "help-block", text: "Please use company email. Personal emails are not allowed as admin!").wait_until_present     
    end
  end
      
  #  -- submit existed in db email: “This email already exists”
  context 'when submit username' do
    it 'then it should be unique' do
      text_field(class: "form-control", name: "username").set "adm.company2@mail.ru"
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "This email already exists").wait_until_present(5)
    end
  end

  ##  - password: 
  #  -- submit empty input:  “Password can't be blank”
  context 'when submit empty password' do
    it 'then password cannot be blank' do
      text_field(name: "password", class: "form-control").set ""
      button(type: "submit",  class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Password can't be blank").wait_until_present(5)
    end
  end

  #  -- submit string with length more than 255:  “Password should contain no more than 255 symbols”
  context 'when submit password' do
    it 'then it should contain no more than 255 symbols' do
      text_field(class: "form-control", name: "password").set str(256)
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Password should contain no more than 255 symbols").wait_until_present(5)     
    end
  end
   
  #  -- submit string with length less than 5 symbols: “Password should be longer than 4 symbols”
  context 'when submit password' do
    it 'then it should contain be longer than 4 symbols' do
      text_field(class: "form-control", name: "password").set "123r"
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Password should be longer than 4 symbols").wait_until_present(5)     
    end
  end

  ## -repeat password (only if password is filled):
  # -- submit any text different from password:  “Passwords do not match”
  context 'when submit password and confiirm it' do
    it 'then it should match' do
      text_field(class: "form-control", type: "password", name: "password").set "123123"
      text_field(class: "form-control", type: "password", name: "confirmationPassword").set "qwerty"
      button(type: "submit", class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Passwords do not match").wait_until_present(5)     
    end
  end


  ##  - organization name: 
  #  -- submit empty input:  “Organization Name can't be blank”
  context 'when submit empty organization name' do
    it 'then Organization Name cannot be blank' do
      text_field(class: "form-control", name: "organizationName" ).set ""
      button(type: "submit",  class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Organization Name can't be blank").wait_until_present(5)
    end
  end

  #  -- submit string with length more than 255:  “Organization Name should contain no more than 255 symbols” 
  context 'when submit organization name' do
    it 'then it should contain no more than 255 symbols' do            
      text_field(class: "form-control", name: "organizationName").set str(256)
      button(class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "Organization Name should contain no more than 255 symbols").wait_until_present  
    end    
  end

  #  -- submit existed in db company name: “This organization name already exists”
  context 'when submit organization name' do
    it 'then it should not already exists' do
      text_field(class: "form-control", name: "organizationName" ).set "ABC-2"
      button(type: "submit",  class: "btn btn-primary btn-block js-submit-btn", text: 'OK').click
      span(class: "help-block", text: "This organization name already exists").wait_until_present(5)
    end
  end
  
end 